#!/bin/bash

allRepos=(01_GeneralInformation \
    02_ProjectOrganization \
    03_ProjectManagement \
    09_WorkingGroups \
    10_JointGroups \
    13_ChangeManagement \
    14_QualityAssurance \
    15_ReleaseManagement \
    17_Communication \
    25_Concepts \
    26_Standards \
    27_Documents \
    28_Processes \
    30_UserGroups \
    80_Tooling \
    )

# echo ${allRepos[@]}
# echo ${#allRepos[@]}
# exit
site="https://svn.autosar.org"
pathRoot="repos/work"
userName="qing.liu@greenstonesoft.com" 

function usage() {
    echo "Usage: $( basename $0 ) [-l | -a | -r <repo1> -r <repo2>]"
    echo "    从${site}的svn仓库检出"
    echo "    -l: 列出可以检查的所有模块"
    echo "    -a: 检出所有模块"
    echo "    -r: 检出指定模块"
    exit 1
}

function checkout_repo() {
    local mods=( $@ )
    local reps=()
    for r in "${mods[@]}"
    do
        reps[${#reps[@]}]="${site}/${pathRoot}/${r}"
    done
    echo svn checkout --username "${userName}" "${reps[@]}" 
    svn checkout --username "${userName}" "${reps[@]}" 
}

mods=()
while getopts ":lar:" arg
do
    case "${arg}" in
        l)
            for r in "${allRepos[@]}"
            do
                echo $r
            done
            exit 
            ;;
        a)
            checkout_repo "${allRepos[@]}"
            exit
            ;;
        r)
            mods[${#mods[@]}]="${OPTARG}"
            ;;
        *)
            usage
            ;;
    esac
done

if [ 0 -eq ${#mods[@]} ]
then
    usage
fi

checkout_repo "${mods[@]}"
